package com.example.demo.mapper;

import com.example.demo.model.Dish;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface DishMapper {

    @Select("SELECT * FROM dish")
    List<Dish> findAll();

    @Select("SELECT * FROM dish WHERE dish_id = #{id}")
    Dish findById(Integer id);

    @Select("SELECT * FROM dish WHERE category_id = #{categoryId}")
    List<Dish> findByCategoryId(Integer categoryId);

    @Select("SELECT * FROM dish WHERE is_recommended = 1")
    List<Dish> findRecommended();

    @Insert("INSERT INTO dish(category_id, name, price, stock, image_url, is_recommended) " +
            "VALUES(#{categoryId}, #{name}, #{price}, #{stock}, #{imageUrl}, #{isRecommended})")
    @Options(useGeneratedKeys = true, keyProperty = "dishId")
    int insert(Dish dish);

    @Update("UPDATE dish SET " +
            "category_id=#{categoryId}, name=#{name}, price=#{price}, " +
            "stock=#{stock}, image_url=#{imageUrl}, is_recommended=#{isRecommended} " +
            "WHERE dish_id=#{dishId}")
    int update(Dish dish);

    @Update("UPDATE dish SET stock = stock - #{quantity} WHERE dish_id = #{dishId}")
    int decreaseStock(@Param("dishId") Integer dishId, @Param("quantity") Integer quantity);

    @Delete("DELETE FROM dish WHERE dish_id=#{id}")
    int delete(Integer id);
}